package shopping.web;

import shopping.bean.Clothes;
import shopping.bean.Page;
import shopping.service.ClothesService;
import shopping.service.impl.ClothesServiceImpl;
import shopping.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ManagerClothesServlet extends BaseServlet{
    private ClothesService clothesService = new ClothesServiceImpl();
    public void getClotheslist(HttpServletRequest req, HttpServletResponse resp){
        //通过ClothesSevlet查询全部图书
        List<Clothes> clothess = clothesService.queryClothess();
        //把全部图书保存到request域中
        req.setAttribute("clothess",clothess);
        //请求转发到pages/manager/clothes_manager.jsp页面
        try {
            req.getRequestDispatcher("/view/manager/clothes_manager.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void delete(HttpServletRequest req,HttpServletResponse resp){
        //获取请求的参数---图书的id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);

        //调用clothesService.deleteClothesById()方法删除图书
        clothesService.deleteClothesById(id);
        //重定向回图书管理页面
        try {
            resp.sendRedirect(req.getContextPath() + "/manage/clothesServlet?action=getClotheslist");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getClothes(HttpServletRequest req,HttpServletResponse resp){
        //获取请求参数图书编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //clothesService.queryClothesById(id);查询要修改的图书信息
        Clothes clothes = clothesService.queryClothesById(id);
        //将该信息保存到request域中
        req.setAttribute("clothes",clothes);
        try {
            req.getRequestDispatcher("/view/manager/clothes_edit.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 0);
        pageNo+=1;
        //1、获取请求的参数==封装成为Clothes对象
        Clothes clothes = WebUtils.copyParamToBean(req.getParameterMap(),new Clothes());
//        2、调用ClothesService.addClothes()保存图书
        clothesService.addClothes(clothes);
//        3、跳到图书列表页面
//                /manager/clothesServlet?action=list
//        req.getRequestDispatcher("/manager/clothesServlet?action=list").forward(req, resp);

        resp.sendRedirect(req.getContextPath() + "/manage/clothesServlet?action=page&pageNo=" + pageNo);

    }

    protected void update(HttpServletRequest req, HttpServletResponse resp){
//        1、获取请求的参数==封装成为Clothes对象
        Clothes clothes = WebUtils.copyParamToBean(req.getParameterMap(),new Clothes());
//        2、调用ClothesService.updateClothes( clothes );修改图书
        clothesService.updateClothes(clothes);
//        3、重定向回图书列表管理页面
//        地址：/工程名/manager/clothesServlet?action=list
        try {
            resp.sendRedirect(req.getContextPath() + "/manage/clothesServlet?action=getClotheslist");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 处理分页功能
     * @param req
     * @param resp
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp){
        //获取请求的参数---pageNo和pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //保存pageNo和pageSize数据到Page对象
        Page<Clothes> page = clothesService.page(pageNo,pageSize);
        page.setUrl("manager/clothesServlet?action=page");
        //保存Page对象到Request域中
        req.setAttribute("page",page);
        //请求转发到pages/manager/clothesmanager.jsp页面
        try {
            req.getRequestDispatcher("/view/manager/clothes_manager.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
